/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.sciCxResult.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.sciCxResult.domain.SciCxResult;
import com.fzhucc.modules.sciCxResult.domain.dto.*;
import com.fzhucc.modules.sciCxResult.service.SciCxResultService;
import com.fzhucc.modules.sciStudio.domain.dto.AllowSciStudioDto;
import com.fzhucc.modules.sciStudio.domain.dto.AuditSciStudioDto;
import com.fzhucc.modules.sciStudio.domain.dto.SciStudioDto;
import com.fzhucc.modules.sciStudio.domain.dto.SciStudioQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2024-01-10
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "创新成果:管理")
@RequestMapping("/api/sciCxResult")
public class SciCxResultController {

    private final SciCxResultService sciCxResultService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sciCxResult:list')")
    public void exportSciCxResult(HttpServletResponse response, SciCxResultQueryCriteria criteria) throws IOException {
        sciCxResultService.download(sciCxResultService.queryAll(criteria), response);
    }

    @Log("我的导出数据")
    @ApiOperation("我的导出数据")
    @GetMapping(value = "/my/download")
    @PreAuthorize("@el.check('sciCxResult:list')")
    public void myexportSciTechnologyDemand(HttpServletResponse response, SciCxResultQueryCriteria criteria) throws IOException {
        sciCxResultService.download(sciCxResultService.queryAllmy(criteria), response);
    }

    @GetMapping
    @Log("查询创新成果")
    @ApiOperation("查询创新成果")
    @PreAuthorize("@el.check('sciCxResult:list')")
    public ResponseEntity<Object> querySciCxResult(SciCxResultQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(sciCxResultService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/list")
    @Log("h5、小程序查询创新成果")
    @ApiOperation("h5、小程序查询创新成果")
    public ResponseEntity<Object> list(SciCxResultQueryCriteria criteria, Pageable pageable) {
        criteria.setIsShow(1);
        return new ResponseEntity<>(sciCxResultService.queryAllh5(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/my")
    @Log("查询我的创新成果")
    @ApiOperation("查询我的创新成果")
    public ResponseEntity<Object> my(SciCxResultQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(sciCxResultService.my(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增创新成果")
    @ApiOperation("新增创新成果")
    //@PreAuthorize("@el.check('sciCxResult:add')")
    public ResponseEntity<Object> createSciCxResult(@Validated @RequestBody SciCxResultAddDto resources){
        return new ResponseEntity<>(sciCxResultService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改创新成果")
    @ApiOperation("修改创新成果")
    @PreAuthorize("@el.check('sciCxResult:edit')")
    public ResponseEntity<Object> updateSciCxResult(@Validated @RequestBody SciCxResultEditDto resources){
        sciCxResultService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除创新成果")
    @ApiOperation("删除创新成果")
    @PreAuthorize("@el.check('sciCxResult:del')")
    public ResponseEntity<Object> deleteSciCxResult(@RequestBody Integer[] ids) {
        sciCxResultService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping(value = "/isAllowEdit")
    @Log("是否允许编辑")
    @ApiOperation("是否允许编辑")
    public ResponseEntity<Object> isAllowEdit(@Validated @RequestBody AllowSciCxResultDto dto) {
        sciCxResultService.isAllowEdit(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping(value = "/getCxResultType")
    @Log("获取字典创新成果类别 cx_result_type")
    @ApiOperation("获取字典创新成果类别  cx_result_type")
    @AnonymousAccess
    public ResponseEntity<Object> getStudioType() {
        return new ResponseEntity<>(sciCxResultService.getCxResultType(), HttpStatus.OK);
    }

    @GetMapping(value = "/getCxResultZlType")
    @Log("获取字典创新成果专利形式 cx_result_zl_type")
    @ApiOperation("获取字典创新成果专利形式  cx_result_zl_type")
    @AnonymousAccess
    public ResponseEntity<Object> getCxResultZlType() {
        return new ResponseEntity<>(sciCxResultService.getCxResultZlType(), HttpStatus.OK);
    }

    @GetMapping(value = "/getCxResultZlStatus")
    @Log("获取字典创新成果专利状况 cx_result_zl_status")
    @ApiOperation("获取字典创新成果专利状况  cx_result_zl_status")
    @AnonymousAccess
    public ResponseEntity<Object> getCxResultZlStatus() {
        return new ResponseEntity<>(sciCxResultService.getCxResultZlStatus(), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个创新成果")
    @ApiOperation("查询单个创新成果")
    public SciCxResultDto queryInfoById(@PathVariable Integer id) {
        return sciCxResultService.findById(id);
    }

    @PutMapping(value = "/audit")
    @Log("创新成果状态审核")
    @ApiOperation("创新成果状态审核")
    public ResponseEntity<Object> auditSciCxcg(@Validated @RequestBody AuditSciCxcgDto dto) {
        sciCxResultService.auditSciCxcg(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}